/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.entity.UserCmdCode;
import com.thienloc.omap.entity.report.SingleSubCpProduction;
import com.thienloc.omap.entity.report.ShortCodeProduction;
import com.thienloc.omap.session.RequestQueueFacade;
import com.thienloc.omap.session.UserCmdCodeFacade;
import com.thienloc.omap.util.JsfUtil;
import java.io.Serializable;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class ProductionByShortCodeBean extends BaseBean implements Serializable {

    @EJB
    private RequestQueueFacade requestQueueFacade;
    @EJB
    private UserCmdCodeFacade userCmdCodeFacade;
    private Date startDate = new Date();
    private Date endDate = new Date();
    private String selectedUsername = "home";
    private String selectedShortCode = "6771";
    private List<ShortCodeProduction> productionMonths = new ArrayList<ShortCodeProduction>();
    private List<ShortCodeProduction> productionDays = new ArrayList<ShortCodeProduction>();
    private List<SingleSubCpProduction> allSubCpProductions = new ArrayList<SingleSubCpProduction>();
    private final Logger logger = LoggerFactory.getLogger(ProductionByShortCodeBean.class);

    @PostConstruct
    public void init() {
        try {
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportByThisMonth() {
        Calendar cal = Calendar.getInstance();
        endDate = cal.getTime();

        cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);
        startDate = cal.getTime();
        report();
    }
    
    public void reportByToday() {
        startDate = new Date();
        endDate = new Date();
        report();
    }

    public void report() {
        if (selectedUsername.equals("all")) {
            productionDays = new ArrayList<ShortCodeProduction>(0);
            reportAllSubCp();
        } else {
            allSubCpProductions = new ArrayList<SingleSubCpProduction>(0);
            reportForOneSubCp();
        }
    }

    public void reportForOneSubCp() {
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);

        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        try {
            String[] blackCmds = null;
            String blackCmdList = userCmdCodeFacade.findByUsername(selectedUsername).getBlackCmdList();
            if (blackCmdList != null && !blackCmdList.isEmpty()) {
                blackCmds = blackCmdList.split(",");
            }
            String[] whiteCmds = null;
            String whiteCmdList = userCmdCodeFacade.findByUsername(selectedUsername).getWhiteCmdList();
            if (whiteCmdList != null && !whiteCmdList.isEmpty()) {
                whiteCmds = whiteCmdList.split(",");
            }

            productionDays = requestQueueFacade.countByShortCode(startDate, endDate, blackCmds, whiteCmds);
            ShortCodeProduction total = new ShortCodeProduction();
            total.setOpertator("Tổng");
            for (ShortCodeProduction shortCodeProduction : productionDays) {
                total.setP6071(shortCodeProduction.getP6071() + total.getP6071());
                total.setP6171(shortCodeProduction.getP6171() + total.getP6171());
                total.setP6271(shortCodeProduction.getP6271() + total.getP6271());
                total.setP6371(shortCodeProduction.getP6371() + total.getP6371());
                total.setP6471(shortCodeProduction.getP6471() + total.getP6471());
                total.setP6571(shortCodeProduction.getP6571() + total.getP6571());
                total.setP6671(shortCodeProduction.getP6671() + total.getP6671());
                total.setP6771(shortCodeProduction.getP6771() + total.getP6771());
            }
            productionDays.add(total);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", "Lỗi");
        }
    }

    public void reportAllSubCp() {
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);

        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        try {
            List<UserCmdCode> legalCmdCodes = userCmdCodeFacade.findLegalSubCp();
            allSubCpProductions = new ArrayList<SingleSubCpProduction>(legalCmdCodes.size());
            for (UserCmdCode userCmdCode : legalCmdCodes) {
                try {
                    SingleSubCpProduction production = requestQueueFacade.countSubCpProduction(startDate, endDate, userCmdCode, selectedShortCode);
                    allSubCpProductions.add(production);
                } catch (Exception ex) {
                    logger.error("[{}]: ", getUsername(), ex);
                    JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
                }
            }
            Collections.sort(allSubCpProductions, new Comparator<SingleSubCpProduction>() {
                @Override
                public int compare(SingleSubCpProduction o1, SingleSubCpProduction o2) {
                    return o1.getSubCpName().compareToIgnoreCase(o2.getSubCpName());
                }
            });
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public List<ShortCodeProduction> getProductionDays() {
        return productionDays;
    }

    public void setProductionDays(List<ShortCodeProduction> productionDays) {
        this.productionDays = productionDays;
    }

    public List<ShortCodeProduction> getProductionMonths() {
        return productionMonths;
    }

    public void setProductionMonths(List<ShortCodeProduction> productionMonths) {
        this.productionMonths = productionMonths;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getSelectedUsername() {
        return selectedUsername;
    }

    public void setSelectedUsername(String selectedUsername) {
        this.selectedUsername = selectedUsername;
    }

    public String getSelectedShortCode() {
        return selectedShortCode;
    }

    public void setSelectedShortCode(String selectedShortCode) {
        this.selectedShortCode = selectedShortCode;
    }

    public List<SingleSubCpProduction> getAllSubCpProductions() {
        return allSubCpProductions;
    }
}